const artcleJson = require('./articles.json')

let tags = []
const dates = []
const titles = []

artcleJson.forEach(article => {
  titles.push(article.title)
  dates.push(article.date)
  tags = tags.concat(article.tags)
})

const articleMap = {
  tagsList: Array.from(new Set(tags)).sort((a, b) => a.localeCompare(b)),
  dates: Array.from(new Set(dates)).sort((a, b) => a - b),
  titles: Array.from(new Set(titles)),
  tag: {}
}

tags.forEach(tag => {
  Reflect.set(
    articleMap.tag,
    tag,
    artcleJson.filter(item => item.tags.includes(tag))
  )
})

module.exports = articleMap